# -*- coding: utf-8 -*-
"""
@Time: 11/20/2018 10:20 PM
@Author: hejing
@Email: 2010jing@gmail.com
"""
from flask import jsonify, g

from app.libs.error_code import NotFound, DeleteSuccess, AuthFailed
from app.libs.redprint import Redprint
from app.libs.token_auth import auth
from app.models.base import db
from app.models.user import User

api = Redprint('user')


# 管理员
@api.route('/<int:uid>', methods=['GET'])
@auth.login_required
def super_get_user(uid):
	# account secret
	# APP
	# token 1month 2 hours
	# token 是否合法 是否过期
	# token
	user = User.query.filter_by(id=uid).get_or_404()
	return jsonify(user)


@api.route('', methods=['GET'])
@auth.login_required
def get_user():
	# account secret
	# APP
	# token 1month 2 hours
	# token 是否合法 是否过期
	# token
	uid = g.user.uid
	user = User.query.filter_by(id=uid).get_or_404()
	return jsonify(user)


# 管理员
@api.route('/<int:uid>', methods=['DELETE'])
def super_delete_user(uid):
	pass


@api.route('', methods=['DELETE'])
@auth.login_required
def delete_user():
	uid = g.user.uid
	with db.auto_commit():
		user = User.query.filter_by(id=uid).first_or_404()
		user.delete()
	return DeleteSuccess()


@api.route('', methods=['PUT'])
def update_user():
	return 'update Abby'

